<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:nyext="urn:noury-extensions">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:param name="package" />
  <xsl:param name="outdir" />
  <xsl:param name="name" />

  <xsl:include href="functions.xslt" />

  <xsl:template name="get-element-name">
    <xsl:param name="element" />
    <xsl:param name="prefix" />
    <xsl:param name="suffix" />
    <xsl:choose>
      <xsl:when test="$element/@shortName">
        <xsl:value-of select="concat($prefix, $element/@shortName, $suffix)" />
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="concat($prefix, $element/@name, $suffix)" />
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>
  
  <xsl:template name="generate-user-file">
    <xsl:text>// $Id$
/*! \file
 * \brief Definition of \c </xsl:text>
      <xsl:value-of select="$package" />
      <xsl:text>.Mappers.
 *
 * \author $Author$
 * \date $Date$
 * \version $Revision$
 */

using System;

namespace </xsl:text>
      <xsl:value-of select="$package" />
      <xsl:text>
{



    ///////////////////////////////////////////////////////////////////////////
    ///
    /// &lt;summary /&gt;
    ///
    ///////////////////////////////////////////////////////////////////////////

    public static partial class Mappers
    {

    }

}
</xsl:text>
  </xsl:template>
  
  <xsl:template name="generate-model-file">
    <xsl:variable name="src">
      <xsl:text>// $Id$
using System;
using System.Data;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;

namespace </xsl:text>
      <xsl:value-of select="$package" />
      <xsl:text>
{

    /// &lt;summary /&gt;
    public static partial class Mappers
    {

        /// &lt;summary /&gt;
        public static DataMapperCollection CreateDataMapperCollection(Database database, ICacheManager cacheManager)
        {
            DataMapperCollection ret=new DataMapperCollection();</xsl:text>
      <xsl:for-each select="/manifest/types/domain" >
        <xsl:text>
            ret.Add(typeof(</xsl:text>
        <xsl:value-of select="@name" />
        <xsl:text>), new </xsl:text>
        <xsl:value-of select="@name" />
        <xsl:text>Mapper(database, cacheManager ?? </xsl:text>
        <xsl:value-of select="@name" />
        <xsl:text>.IdentityMap.CacheManager));</xsl:text>
      </xsl:for-each>
      <xsl:text>

            return ret;
        }
    }

}
</xsl:text>
    </xsl:variable>
    <xsl:value-of select="nyext:generateFile($src, nyext:combinePath($outdir, concat(nyext:getModelFileName($name), '.cs')))" />
  </xsl:template>

  <xsl:template match="/">
    <xsl:call-template name="generate-user-file" />
    <xsl:call-template name="generate-model-file" />
  </xsl:template>
</xsl:stylesheet>
